<script lang="ts" setup>
import { computed } from 'vue';
import { formatNumber } from '@/home/utils/format';
import { useFetch } from '@/home/hooks/fetch';
import { useAsyncData } from '@/home/hooks/async-data';
import TabNavigation from '@/home/components/TabNavigation.vue';
import Pagination from '@/home/components/Pagination.vue';
import { useTitleForClient } from '@/home/hooks/browser';
import LoadingBox from '@/home/components/LoadingBox.vue';
import Loading from '@/home/components/Loading.vue';
import Tag from '@/home/components/Tag.vue';
import { useI18n } from 'vue-i18n';
import { getShortDate } from '@/home/utils/date';

const atomgitUrl = import.meta.env.VITE_ATOMGIT_URL;

const { t } = useI18n();
const { $fetch } = useFetch();
const { setTitle } = useTitleForClient();

const pathPrefix = '/explore/journalism/';
const defaultTitle = t('home.explore.journalism.title');
const props = defineProps({
  type: {
    type: String,
    default: '',
  },

  page: {
    type: String,
    default: '',
  },
});

const PAGE_SIZE = 10;
const types = [
  {
    name: 'all',
    value: '-1',
    text: t('home.common.all'),
  },
  {
    name: 'prime',
    value: '1',
    text: t('home.common.prime'),
  },
];

const getCurrentType = () => types.find((item) => item.name === props.type) ?? types[0];
const getTitle = () => `${getCurrentType().text} - ${defaultTitle}`;

const currentType = computed(() => getCurrentType().name);

const { data: listing, pending } = useAsyncData(
  'explore:journalism',
  () => $fetch('/information/page', {
    method: 'post',
    data: {
      pageNum: Number(props.page) || 1,
      pageSize: PAGE_SIZE,
      isSelected: getCurrentType().value,
    },
  }),
  {
    cache: false,
    transform: (data) => data ?? {},
    watch: [() => props.type, () => props.page],
    onAfterHandle({ handleSuccess, serverContext }) {
      if (!handleSuccess) {
        return;
      }

      if (serverContext) {
        serverContext.title = getTitle();
      }

      if (!import.meta.env.SSR) {
        setTitle(getTitle());
      }
    },
  },
);
const getRouterPath = (pageNum: number) => `${pathPrefix}${currentType.value}/${pageNum}`;
</script>

<template>
  <section class="page-explore-journalism mt-40 container">
    <TabNavigation :path-prefix="pathPrefix" :items="types" v-model="currentType" />

    <ul class="mt-20 min-h-200 rounded-4 bg-white p-20 shadow-[0_1px_6px_0_rgba(0,0,0,0.04)]">
      <LoadingBox :loading="pending">
        <template v-if="listing?.records?.length > 0">
          <li class="mb-20 border-b-1 border-gray-2 bg-white pb-20 last-of-type:(mb-0 border-b-0 pb-0)" v-for="item in listing.records" :key="item.informationId">
            <div class="flex-y-center flex-wrap gap-x-4 text-12 text-[#adadad] leading-20">
              <template v-if="item.authorInfo">
                <a :href="`${atomgitUrl}/${item.authorInfo.username}`" target="_blank" class="flex-y-center gap-x-4" @click.stop>
                  <img
                    :data-src="item.authorInfo.photo"
                    :alt="item.authorInfo.nickname"
                    class="lozad block size-20 border border-#e8e8e8 rounded-50% border-solid"
                    v-if="item.authorInfo.photo"
                  >
                  <div class="text-#292929">{{ item.authorInfo.nickname }}</div>
                </a>
                <div>·</div>
              </template>
              <div>{{ getShortDate(item.publishTime) }}</div>
            </div>

            <RouterLink :to="`/explore/journalism/detail/${item.informationId}`" class="group mt-8 block w-full" target="_blank">
              <div class="mb-10 flex justify-between gap-x-20">
                <div class="flex-grow">
                  <div class="mb-6 flex">
                    <div v-if="item.isSelected == '1'" class="mr-8 h-22 flex-shrink-0 rounded-2 bg-hex-E8F4FF px-6 text-center text-12 color-hex-345DE2 font-400 leading-22">{{ $t('home.common.prime') }}</div>
                    <h3 class="line-clamp-1 break-anywhere text-16 color-hex-292929 font-500 leading-22 group-hover:(text-color-primary-2)">{{ item.informationTitle }}</h3>
                  </div>
                  <p class="line-clamp-3 break-anywhere text-justify text-pretty text-14px color-hex-575757 font-400 leading-22">{{ item.informationMsg }}</p>
                </div>
                <div class="h-94 w-160 flex-shrink-0" v-if="item.coverUrl">
                  <img :data-src="item.coverUrl" :alt="item.informationTitle" class="lozad size-full rounded-4 bg-#f7f7f7 object-contain">
                </div>
              </div>
            </RouterLink>

            <div class="flex-y-center justify-between gap-x-20">
              <div class="flex-shrink-0 self-end text-12 color-hex-8b8b8b font-400 leading-22">
                {{ $t('home.explore.journalism.read', { view: formatNumber(item.informationViews) }) }}
              </div>

              <div class="flex flex-wrap justify-end gap-x-10 gap-y-6">
                <Tag
                  v-for="piece in item.labelNameList"
                  :key="piece.labelId"
                  :id="piece.labelId"
                  :name="piece.labelName"
                  class="block rounded-11 bg-hex-f7f7f7 px-12 py-3 text-center text-12 color-hex-8b8b8b font-400 leading-16 hover:(bg-hex-e8e8e8)"
                  blank
                />
              </div>
            </div>
          </li>
        </template>
        <template v-else-if="listing?.records?.length === 0">
          <li class="min-h-100 text-center color-hex-8b8b8b">{{ $t('home.common.emptyContent') }}</li>
        </template>

        <template #loading>
          <li class="min-h-100 flex-center">
            <Loading class="size-16 color-hex-8b8b8b" />
          </li>
        </template>
      </LoadingBox>
    </ul>

    <Pagination class="data-[total-pages=gt1]:(mt-20)" :count="listing?.total" :page="Number(props.page) || 1" :page-size="PAGE_SIZE" :get-router-path="getRouterPath" />
  </section>
</template>

<style lang="scss" scoped>
</style>
